package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.manager.mapper.SysUserMapper;
import com.atguigu.spzx.manager.service.SysUserService;
import com.atguigu.spzx.model.dto.system.LoginDto;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.system.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.UUID;

@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    SysUserMapper sysUserMapper;
    @Override
    public LoginVo login(LoginDto loginDto, String ip) {
        String userName = loginDto.getUserName();
        String password = loginDto.getPassword();
        SysUser sysUser = sysUserMapper.selectSysUserByName(userName);

        if (null == sysUser) {
            return null;
        }

        String md5password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!md5password.equals(sysUser.getPassword())) {
            return null;
        }

        LoginVo loginVo = new LoginVo();
        String token = UUID.randomUUID().toString().replace("-", "");
        // 存入缓存一份
        loginVo.setToken(token);

        return loginVo;
    }
}
